package jp.co.hcs.attendancemanagement.inputdailyattendance.action;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.hcs.attendancemanagement.AMConstants;
import jp.co.hcs.attendancemanagement.action.AMLookupDispatchAction;
import jp.co.hcs.attendancemanagement.exception.AMException;
import jp.co.hcs.attendancemanagement.inputdailyattendance.actionform.InputDailyAttendanceForm;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.ConfirmDailyWorkingLogic;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.DeleteDailyWorkingLogic;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.InputDailyAttendanceLogic;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.ReCalculateDailyWorkingLogic;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.SaveDailyWorkingLogic;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.dto.ProjectInfoDTO;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

/**
 * The Class InputDailyAttendanceAction.
 */
public class InputDailyAttendanceAction extends AMLookupDispatchAction {

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.struts.actions.LookupDispatchAction#getKeyMethodMap()
   */
  protected Map getKeyMethodMap() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("inputdailyattendace.button.show", "show");
    map.put("inputdailyattendace.button.delete", "delete");
    map.put("inputdailyattendace.button.save", "save");
    map.put("inputdailyattendace.button.cofirm", "cofirm");
    map.put("inputdailyattendace.button.reCaculate", "reCaculate");
    map.put("inputdailyattendace.button.addProject", "addProject");
    map.put("inputdailyattendace.button.deleteProject", "deleteProject");
    return map;
  }

  /**
   * Reset flag.
   * 
   * @param form the form
   */
  private void resetFlag(InputDailyAttendanceForm form) {
    form.setDeleteSuccess(false);
    form.setSaveSuccess(false);
    form.setConfirmSuccess(false);
  }

  /**
   * Show.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {

    String userId = (String) request.getSession().getAttribute(AMConstants.USER_LOGGED_IN);

    if (userId == null) {
      return mapping.findForward(LOGIN);
    }

    InputDailyAttendanceLogic inputDailyAttendanceLogic = InputDailyAttendanceLogic.getInstance();
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    inputDailyAttendanceForm.setSubstituteHoliday("");
    resetFlag(inputDailyAttendanceForm);
    inputDailyAttendanceForm.setUserId(userId);
    ActionMessages errors = new ActionMessages();
    try {
      errors = inputDailyAttendanceLogic.initDataForScreen(inputDailyAttendanceForm);
    } catch (SQLException e) {
      throw new AMException(e);
    }
    inputDailyAttendanceForm.setAttendanceSettingList(inputDailyAttendanceLogic
        .getAttendanceSettingList());
    inputDailyAttendanceForm.setTimeSettingList(inputDailyAttendanceLogic.getTimeSettingList());

    saveErrors(request, errors);
    
    return mapping.findForward(SUCCESS);
  }

  /**
   * Delete.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    DeleteDailyWorkingLogic deleteLogic = new DeleteDailyWorkingLogic();
    InputDailyAttendanceLogic inputDailyAttendanceLogic = InputDailyAttendanceLogic.getInstance();
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    resetFlag(inputDailyAttendanceForm);
    ActionMessages errors = deleteLogic.deleteDailyWorking(inputDailyAttendanceForm);
    inputDailyAttendanceForm.setFindRestDetailFlag(false);
    inputDailyAttendanceForm.setRestTimeInfoDTOList(null);
    inputDailyAttendanceForm.setDeleteSuccess(errors.isEmpty());
    
    if (errors.isEmpty()) {
        inputDailyAttendanceForm.setSubstituteHoliday("");
    }
    
    // initialize data
    inputDailyAttendanceLogic.initDataForScreen(inputDailyAttendanceForm);
    
    saveErrors(request, errors);
    return mapping.findForward(SUCCESS);
  }

  /**
   * Save.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    SaveDailyWorkingLogic saveLogic = new SaveDailyWorkingLogic();
    InputDailyAttendanceLogic inputDailyAttendanceLogic = InputDailyAttendanceLogic.getInstance();
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    resetFlag(inputDailyAttendanceForm);
    ActionMessages errors = saveLogic.saveDailyWorking(inputDailyAttendanceForm);
    inputDailyAttendanceForm.setFindRestDetailFlag(false);
    inputDailyAttendanceForm.setRestTimeInfoDTOList(null);
    inputDailyAttendanceForm.setSaveSuccess(errors.isEmpty());
    
    // initialize data
    inputDailyAttendanceLogic.initDataForScreen(inputDailyAttendanceForm);
    
    saveErrors(request, errors);
    return mapping.findForward(SUCCESS);
  }

  /**
   * Cofirm.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public ActionForward cofirm(ActionMapping mapping, ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    ConfirmDailyWorkingLogic confirmLogic = new ConfirmDailyWorkingLogic();
    InputDailyAttendanceLogic inputDailyAttendanceLogic = InputDailyAttendanceLogic.getInstance();
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    resetFlag(inputDailyAttendanceForm);
    ActionMessages errors = confirmLogic.confirmDailyWorking(inputDailyAttendanceForm);
    inputDailyAttendanceForm.setFindRestDetailFlag(false);
    inputDailyAttendanceForm.setRestTimeInfoDTOList(null);
    inputDailyAttendanceForm.setConfirmSuccess(errors.isEmpty());
    
    // initialize data
    inputDailyAttendanceLogic.initDataForScreen(inputDailyAttendanceForm);
    
    saveErrors(request, errors);
    return mapping.findForward(SUCCESS);
  }

  /**
   * Re caculate.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public ActionForward reCaculate(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    ReCalculateDailyWorkingLogic reCalculateLogic = new ReCalculateDailyWorkingLogic();
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    resetFlag(inputDailyAttendanceForm);
    String userId = (String) request.getSession().getAttribute(AMConstants.USER_LOGGED_IN);
    inputDailyAttendanceForm.setUserId(userId);
    ActionMessages errors = new ActionMessages();
    try {
      errors = reCalculateLogic.reCalculate(inputDailyAttendanceForm);
    } catch (SQLException e) {
      throw new AMException(e);
    }
    saveErrors(request, errors);
    return mapping.findForward(SUCCESS);
  }

  /**
   * Adds the project.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public ActionForward addProject(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    resetFlag(inputDailyAttendanceForm);
    List<ProjectInfoDTO> projectEffortInfoDTOList =
        inputDailyAttendanceForm.getProjectInfoDTOList();
    ProjectInfoDTO projectEffortInfoDTONew = new ProjectInfoDTO();
    projectEffortInfoDTOList.add(projectEffortInfoDTONew);
    return mapping.findForward(SUCCESS);
  }

  /**
   * Delete project.
   * 
   * @param mapping the mapping
   * @param form the form
   * @param request the request
   * @param response the response
   * @return the action forward
   * @throws Exception the exception
   */
  public ActionForward deleteProject(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    resetFlag(inputDailyAttendanceForm);
    List<ProjectInfoDTO> projectEffortInfoDTOList =
        inputDailyAttendanceForm.getProjectInfoDTOList();
    if (projectEffortInfoDTOList != null && projectEffortInfoDTOList.size() != 0) {
      Iterator<ProjectInfoDTO> iterator = projectEffortInfoDTOList.iterator();
      ProjectInfoDTO projectEffortInfoDTOtemp;
      while (iterator.hasNext()) {
        projectEffortInfoDTOtemp = iterator.next();
        if (projectEffortInfoDTOtemp.getSelect() != null && projectEffortInfoDTOtemp.getSelect()) {
          iterator.remove();
        }
      }
    }
    return mapping.findForward(SUCCESS);
  }

}
